﻿using System;
using System.Linq;
using gbL.NetBoard.Examples.DoubleMVC.View.UI.Requests;

namespace gbL.NetBoard.Examples.DoubleMVC.Controller.Utils
{
    static class MenuControllerHelpers
    {
        internal static ControllerDispatch ChooseControllerDispatch(DispatchOption[] options, IInputSource inputSource)
        {
            char input = default(char);
            char[] indexes = options.Select(x => x.Index).ToArray();
            while (Array.IndexOf(indexes, input) < 0)
                input = Char.ToUpper(inputSource.GetKey());
            return new ControllerDispatch(
                options.Single(x => x.Index.Equals(input)).Action
            );
        }

        internal static MenuViewRequest BuildMenu(string title, DispatchOption[] options)
        {
            MenuViewRequest request = new MenuViewRequest();
            request.MenuChoices = options.Select(x => x.Index).ToArray();
            request.MenuTitle = title;
            request.Content = options.Aggregate(
                string.Empty,
                (content, option) => string.Format("{0}\r\n{1}: {2}", content, option.Index, option.Title)
            );
            return request;
        }

    }
}
